﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Utils;
using System.Data.SqlClient;
using Jungle.Code.DAL;
using Jungle.Code.BLL;


namespace Jungle.Modules.VisionEditor
{
    public partial class adminModule : BaseAdminModule
    {
        int sectionID;
        protected void Page_Load(object sender, EventArgs e)
        {
            Application["FCKeditor:UserFilesPath"] = ResolveUrl("~/UserFiles/");
            editor.Debug = false;

            editor.BasePath = "~/modules/fckeditor/";
            if (!this.IsPostBack)
                if (Request["sectionID"] != null)
                {
                    sectionID = int.Parse(Request["sectionID"]);
                    editor.Value = GetContent(sectionID);
                    Save.CommandArgument = sectionID.ToString();
                }
        }
        public string GetContent(int sectionID)
        {
            string cont = "";
            SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
            using (SqlCommand command = new SqlCommand("dbo.mdVisionEditor_GetBySectionID", conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("sectionID", SqlDbType.Int).Value = sectionID;
                conn.Open();
                SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                if (dr.HasRows)
                {
                    dr.Read();
                    cont = (string)dr["content"];
                }
                else
                    Insert(sectionID);
                dr.Close();
            }
            return cont;
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string str = editor.Value;
            int sectionID = int.Parse(btn.CommandArgument);
            if (str == null) str = " ";
            UpdateContent(sectionID, str);
            Response.Redirect("~/admin/Default.aspx?nodeID=" + Request["nodeID"] + "&cmd=cont");

        }
        public bool Insert(int sectionID)
        {
            SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
            using (SqlCommand command = new SqlCommand("dbo.mdVisionEditor_Insert", conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("sectionID", SqlDbType.Int).Value = sectionID;
                conn.Open();
                command.ExecuteNonQuery();
                conn.Close();
            }
            return true;
        }
        public bool UpdateContent(int sectionID, string content)
        {
            SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
            using (SqlCommand command = new SqlCommand("dbo.mdVisionEditor_UpdateBySectionID", conn))
            {
                if (content == null) content = " ";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("sectionID", SqlDbType.Int).Value = sectionID;
                command.Parameters.Add("content", SqlDbType.Text).Value = content;
                conn.Open();
                command.ExecuteNonQuery();
                conn.Close();
            }
            return true;
        }

        public override void FillBaseInfo()
        {
        }
        public override void Main()
        {
        }

        
    }
}